//
//  ST_TIANComponentCollection.swift
//  btvideo
//
//  Created by Jason on 2021/11/2.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import Foundation
import RxSwift
import SwiftyUserDefaults

let SCComponets = ST_TIANComponentCollection.default

class ST_TIANComponentCollection {
    
    static let `default` = ST_TIANComponentCollection()
    
    private init(){}
    
    /// 基础配置[必须/required]
    lazy var config_component = ST_TIANRequirementConfigComponent()
    ///延迟任务
    lazy var delaytask = ST_TIANDelayTaskComponent()
    ///emoji 加载
    lazy var emoji_component = ST_TIANEmojiTaskComponent()
    /// 文案等存储
    lazy var rich_text_storage_component = ST_TIANRichTextStorageComponent()
    /// 资源图片大小配置
    let asset_config = ST_TianAssetConfigComponent()
    /// 定位
    lazy var location_component = ST_TianLocationComponent()
    
    /// 进入首页后 弹窗管理
    private(set) var alert_dispatcher:ST_TIANAlertDispatchComponent?
    
    var disposeBag = DisposeBag()
}

extension ST_TIANComponentCollection{
    
    func start(){
        
        config_component.setup_boot_config()
        
        login_status_did_changed_observable().distinctUntilChanged().subscribe(onNext: { f in
            
            if f ,let user = ST_BTUserInfoManager.shared.current_account_info?.userinfo {
                self.alert_dispatcher = ST_TIANAlertDispatchComponent(myInfo: user)
            }else{
                self.alert_dispatcher = nil
            }
            
         }).disposed(by: disposeBag)
        
        
    }
    
    /// 登录状态切换 拉取数据/清除数据
    func login_status_did_changed_observable()->Observable<Bool>{
        
        return Observable.combineLatest(SCComponets.config_component.boot_config_observer.filter{ $0 == .finished }.mapToVoid(), Defaults.rx.observe(Bool.self, DefaultsKeys.BTLoginSuccess._key)).map{ $1 == true }
        
    }
    
    
}



